<?php

namespace App\Http\Controllers\Backstage;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Announcements;
use App\Services\JPush\JPushService;

class AnnouncementController extends Controller
{
    /**
     * 获取公告列表
     * @return $this
     */
    public function getList(){
        $announcements = Announcements::where('state',1)->orderBy('updated_at','desc')->paginate(15);
        return view('backstage.announcements.list')
            ->with('announcements',$announcements);
    }

    /**
     * 添加公告
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function getAdd(){
        return view('backstage.announcements.add');
    }

    /**
     * 保存公告
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function postAdd(Request $request){
        $this->validate($request,[
            'announcement_content' => 'required|string|max:255',
            'announcement_begin_time' => 'required|date',
            'announcement_end_time' => 'required|date'
        ]);
        $announcement_content = trim($request->input('announcement_content'));
        $announcement_begin_time = trim($request->input('announcement_begin_time'));
        $announcement_end_time = trim($request->input('announcement_end_time'));
        $is_send_message = intval($request->input('is_send_message')) == 1 ? 1 : 0;
        $announcement_begin_time = strtotime($announcement_begin_time.' 00:00:00');
        $announcement_end_time = strtotime($announcement_end_time.' 23:59:59');
        $announcement_detail_content = trim($request->input('announcement_detail_content'));
        $announcement_type = intval($request->input('announcement_type')) == 0 ? 1 : intval($request->input('announcement_type'));
        if($announcement_end_time <= $announcement_begin_time){
            return redirect()->back()->with('error_message','开始时间不能大于结束时间');
        }
        $announcements = new Announcements();
        $announcements->announcement_content = $announcement_content;
        $announcements->announcement_begin_time = $announcement_begin_time;
        $announcements->announcement_end_time = $announcement_end_time;
        $announcements->announcement_type = $announcement_type;
        $announcements->announcement_detail_content = empty($announcement_detail_content) ? '' : $announcement_detail_content;
        $announcements->is_send_message = $is_send_message;
        $announcements->updated_at = time();
        $announcements->created_at = time();
        $announcements->save();
        if($is_send_message == 1){
            $data_json = json_encode([
                'notice_title' => $announcement_content,
                'platform' => 0,
                'user_id' => 0,
                'extras' => [
                    'title' => '系统公告',
                    'user_id' => 0,
                    'class' => 'system_notification',
                    'type' => 1,
                    'jump_value' => '',
                    'template_no' => 2,
                    'message' => $announcement_content,
                    'images'=>[]
                ]
            ]);
            $send_time = strtotime(date('Y-m-d',$announcement_begin_time).' 10:00:00');
            $arr = array('send_time'=>$send_time,'app_id'=>10,'resource_id'=>$announcements->announcement_id,'data_json'=>$data_json);
            $result = JPushService::add_timing_job($arr);
        }
        return redirect()->back()->with('success_message','保存成功');
    }

    /**
     * 更新公告
     * @param Request $request
     * @return $this
     */
    public function getUpdate(Request $request){
        $announcement_id = intval($request->input('announcement_id'));
        $announcements = Announcements::find($announcement_id);
        return view('backstage.announcements.update')->with('announcements',$announcements);
    }

    /**
     * 更新公告内容
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function postUpdate(Request $request){
        $this->validate($request,[
            'announcement_content' => 'required|string|max:255',
            'announcement_begin_time' => 'required|date',
            'announcement_end_time' => 'required|date'
        ]);
        $announcement_id = intval($request->input('announcement_id'));
        $announcements = Announcements::find($announcement_id);
        if(empty($announcements)){
            return redirect()->back()->with('error_message','未找到公告');
        }
        $announcement_content = trim($request->input('announcement_content'));
        $announcement_begin_time = trim($request->input('announcement_begin_time'));
        $announcement_end_time = trim($request->input('announcement_end_time'));
        $is_send_message = intval($request->input('is_send_message')) == 1 ? 1 : 0;
        $announcement_begin_time = strtotime($announcement_begin_time.' 00:00:00');
        $announcement_end_time = strtotime($announcement_end_time.' 23:59:59');
        $announcement_detail_content = trim($request->input('announcement_detail_content'));
        $announcement_type = intval($request->input('announcement_type')) == 0 ? 1 : intval($request->input('announcement_type'));

        $announcements->announcement_content = $announcement_content;
        $announcements->announcement_type = $announcement_type;
        $announcements->announcement_detail_content = empty($announcement_detail_content) ? '' : $announcement_detail_content;
        $announcements->announcement_begin_time = $announcement_begin_time;
        $announcements->announcement_end_time = $announcement_end_time;
        $announcements->is_send_message = $is_send_message;
        $announcements->updated_at = time();
        $announcements->save();
        JPushService::delete_timing_job(array('app_id'=>10,'resource_id'=>$announcement_id));
        if($is_send_message == 1){
            $data_json = json_encode([
                'notice_title' => $announcement_content,
                'platform' => 0,
                'user_id' => 0,
                'extras' => [
                    'title' => '系统公告',
                    'user_id' => 0,
                    'class' => 'system_notification',
                    'type' => 1,
                    'jump_value' => '',
                    'template_no' => 2,
                    'message' => $announcement_content,
                    'images'=>[]
                ]
            ]);
            $send_time = strtotime(date('Y-m-d',$announcement_begin_time).' 10:00:00');
            $arr = array('send_time'=>$send_time,'app_id'=>10,'resource_id'=>$announcements->announcement_id,'data_json'=>$data_json);
            $result = JPushService::add_timing_job($arr);
        }
        return redirect('backstage/announcements/list')->with('success_message','修改成功');
    }

    /**
     * 删除公告
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function getDelete(Request $request){
        $announcement_id = intval($request->input('announcement_id'));
        $announcements = Announcements::find($announcement_id);
        if(empty($announcements)){
            return redirect()->back()->with('error_message','未找到公告');
        }
        //$announcements = new Announcements();
        $announcements->state = 2;
        $announcements->updated_at = time();
        $announcements->save();
        //删除消息列表
        $result = JPushService::delete_timing_job(array('app_id'=>10,'resource_id'=>$announcement_id));
        return redirect()->back()->with('success_message','删除成功');
    }
}
